<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Article;
use Markdown;
use Storage;
use Image;

class ArticlesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
         $articles = Article::orderBy('created_at','asc')->paginate(5);
        return view('art.index', ['articles'=>$articles]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('art.mk_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //数据验证 错误处理
       
        $this->validate($request,[
            'title'=>'required|max:50',
            'content'=>'required|max:1500',
            ]);
        //orm方式写入
        $article = Article::create([
            'title'=>$request->title,
            'content'=>$request->content,
            ]);
        //或者
       /*  $article = new Article();
         $article->title =$request->title;
         $article->content = $request->content;
         $article->save();*/

         //db方式写入
         //insert()方法返回值为true 和 false
         //$res = DB::table('articles')->insert(['title'=>$request->title,'content'=>$request->content]);
        return redirect()->route('blog.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $article = Article::findOrFail($id);
        return view('art.show',['article'=>$article]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $article = Article::findOrFail($id);
        return view('art.edit',['article'=>$article]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' => 'required|max:50',
            'content'=>'required|max:500',
        ]);

        $article = Article::findOrFail($id);
        $article->update([
            'title' => $request->title,
            'content' => $request->content,
        ]);

         return redirect()->route('blog.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $article = Article::findOrFail($id);
        $article->delete();
        return back();
    }


    //显示markdown
      public function MKshow($id)
    {
        $article = Article::findOrFail($id);

        $article->content = Markdown::convertToHtml($article->content);

        return view('art.show',['article'=>$article]);
    }
    /**
     * 处理ajax markdown 阅览
     */
    public function markdown(Request $request)
    {
        return Markdown::convertToHtml($request->content);
    }


    public function  photo(Request $request)
    {

        // dd($request->photo);
            //生成路径，图片存储
            $prefix = "avatar".mt_rand(1,1000);
            $photo = $request->photo;
            $Origname = $photo->getClientOriginalName();
            $name = $prefix.$Origname;
            $src = "images/avatar/".$name;
            $realPath = $request->photo->getRealPath();
            Storage::disk('uploads')->put($src,file_get_contents($realPath));

           // Image::make($photo)->save(public_path($src));
            echo  $src;
        }


    public function avater()
    {
        return view('art.avatar');
    }
}
